//
//  ScrollViewAppDelegate.m
//  ScrollView
//
//  Created by Neelam Roy on 4/8/10.
//  Copyright __MyCompanyName__ 2010. All rights reserved.
//
#import <AdSupport/ASIdentifierManager.h>
#import "ScrollViewAppDelegate.h"
#import "ScrollViewViewController.h"
#import "FirstLoadViewController.h"
#import "StoreViewController.h"
#import "InforViewController.h"
#import "SearchViewController.h"
@implementation ScrollViewAppDelegate

@synthesize window = _window;
@synthesize tabBarController = _tabBarController;
@synthesize langId;
@synthesize viewController = _viewController;
@synthesize navCtrlr;
- (void)applicationDidFinishLaunching:(UIApplication *)application {    
    
    
//    if (NSClassFromString(@"ASIdentifierManager")) {
//        NSLog(@"GoogleAdMobAdsSDK ID for testing: %@" ,[[[ASIdentifierManager sharedManager]
//                advertisingIdentifier] UUIDString]);
//    } else {
//        NSLog(@"GoogleAdMobAdsSDK ID for testing: %@" ,[[UIDevice currentDevice] uniqueIdentifier]);
//    }
    
    ///language
    NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
    langId = [defaults objectForKey:kLanguage];
    if (langId == nil) {
        //first run app
        [self setLanguage:@"vn"];
    }
        
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    self.viewController = [[[SearchViewController alloc] initWithNibName:@"SearchViewController" bundle:nil] autorelease];
    [self.window makeKeyAndVisible];
    navCtrlr = [[UINavigationController alloc]initWithRootViewController:self.viewController];
    [self.window setRootViewController:navCtrlr];
    //[self.window addSubview:[navCtrlr view]];
    navCtrlr.delegate = self;
    navCtrlr.navigationBar.barStyle = UIBarStyleBlackOpaque;
    [self.window makeKeyAndVisible];
     navCtrlr.navigationBarHidden = YES;
}
- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
   
    
    NSString* cname = NSStringFromClass([viewController class]);
    if([cname isEqualToString:@"SearchViewController"] )
    {
        
        APP_DELEGATE.navCtrlr.navigationBarHidden=YES;
        [[SearchViewController getSearchViewControllerInstance] positionLanguage];
    }
    else if([cname isEqualToString:@"StoreViewController"] )
    {
        APP_DELEGATE.navCtrlr.navigationBarHidden=NO;
        [[StoreViewController getStoreViewControllerInstance] positionLanguage];
        
    }

}
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
   
    
    return (1 << UIInterfaceOrientationPortrait) | (1 << UIInterfaceOrientationLandscapeLeft) | (1 << UIInterfaceOrientationLandscapeRight) | (1 << UIInterfaceOrientationPortraitUpsideDown);
}
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    
    NSString* cname = NSStringFromClass([viewController class]);
    if([cname isEqualToString:@"SearchViewController"] )
    {
    
        APP_DELEGATE.navCtrlr.navigationBarHidden=YES;
        if([SearchViewController getSearchViewControllerInstance].ipTabBar!=nil)
        {
            UITabBarItem *item= [[SearchViewController getSearchViewControllerInstance].ipTabBar.items objectAtIndex:0];        
            [[SearchViewController getSearchViewControllerInstance].ipTabBar setSelectedItem:item];
        }
        [[SearchViewController getSearchViewControllerInstance] positionLanguage];
           
    }
    else if([cname isEqualToString:@"StoreViewController"] )
    {
        APP_DELEGATE.navCtrlr.navigationBarHidden=NO;
        [[StoreViewController getStoreViewControllerInstance] repairData];
       [[StoreViewController getStoreViewControllerInstance] positionLanguage];
    }
        

}
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
{
    
}
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
{
    return TRUE;
}

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
    
}
- (void)setLanguage:(NSString*)lang
{
    self.langId = lang;
    
    NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:lang forKey:kLanguage];
    [defaults synchronize];
    
}
- (void)applicationWillResignActive:(UIApplication *)application
{
    /*
     Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
     Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
     */
}

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    /*
     Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 
     If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
     */
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    /*
     Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
     */
}

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    /*
     Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
     */
}

- (void)applicationWillTerminate:(UIApplication *)application
{
    /*
     Called when the application is about to terminate.
     Save data if appropriate.
     See also applicationDidEnterBackground:.
     */
}

/*
 // Optional UITabBarControllerDelegate method.
 - (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
 {
 }
 */

/*
 // Optional UITabBarControllerDelegate method.
 - (void)tabBarController:(UITabBarController *)tabBarController didEndCustomizingViewControllers:(NSArray *)viewControllers changed:(BOOL)changed
 {
 }
 */


- (void)dealloc {
    [_window release];
   // [_tabBarController release];
    [_viewController release];
    [navCtrlr release];
    [super dealloc];}


@end
